using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Web;
using System.Xml;

namespace AliCMS.SubscribeModule.Bll
{
    public partial class Company : DbObjectBase, ICompany
    {
        public class Tag : DbObjectBase, SubscribeModule.Company.ITag
        {
            public Tag(int instanceID, string lang, bool checkRight, string database)
                : base(instanceID, lang, checkRight, database)
            {
            }

            public override object ExecuteService(HttpContext context)
            {
                object result = null;

                string operation = context.Request.QueryString["Operation"];
                if (operation == "Add")
                {
                    Dictionary<string, object> item = new Dictionary<string, object>();
                    item[Alias.Company.Tag.CompanyID] = context.Request.Form["CompanyID"];
                    item[Alias.Company.Tag.TagID] = context.Request.Form["TagID"];

                    result = Add(item);
                }
                else if (operation == "Delete")
                {
                    string id = context.Request.QueryString["ID"];

                    if (id != null)
                    {
                        string[] ids = id.Split('|');
                        for (int i = 0; i < ids.Length; i++)
                        {
                            if (String.IsNullOrEmpty(ids[i]))
                                continue;

                            Delete(Convert.ToInt32(ids[i]));
                        }
                    }
                    else
                    {
                        Guid companyID = new Guid(context.Request.QueryString["CompanyID"]);
                        Guid tagID = new Guid(context.Request.QueryString["TagID"]);
                        Delete(companyID, tagID);
                    }
                }
                else if (operation == "List")
                {
                    string value = null;

                    int page = -1;
                    value = context.Request.QueryString["Page"];
                    if (value != null) page = Convert.ToInt32(value);

                    int pageSize = -1;
                    value = context.Request.QueryString["PageSize"];
                    if (value != null) pageSize = Convert.ToInt32(value);

                    string condition = context.Request.QueryString["Condition"];
                    string sort = context.Request.QueryString["Sort"];

                    result = List(page, pageSize, condition, sort);
                }
                else
                {
                    throw new Exception("lang_header_param");
                }

                return result;
            }

            public int GetInnerIDFromGuid(Guid guid)
            {
                return Dal.GetInnerIDFromGuid(guid);
            }
            public Guid GetGuidFromInnerID(object id)
            {
                return Dal.GetGuidFromInnerID(id);
            }
            public Dictionary<string, object> Get(object id)
            {
                if (CheckRight)
                {
                    Guid guid;
                    if (id is Guid)
                        guid = (Guid)id;
                    else
                        guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Get", guid);
                }

                return Dal.Get(id);
            }
            public Guid Add(Dictionary<string, object> item)
            {
                if (CheckRight)
                {
                    ValidateRight("Add", Guid.Empty);
                }

                if (!item.ContainsKey(Alias.Company.Tag.ID) || item[Alias.Company.Tag.ID].ToString() == String.Empty)
                    item[Alias.Company.Tag.ID] = Guid.NewGuid();
                else if (!(item[Alias.Company.Tag.ID] is Guid))
                    item[Alias.Company.Tag.ID] = new Guid(item[Alias.Company.Tag.ID].ToString());

                if (!(item[Alias.Company.Tag.CompanyID] is Guid))
                    item[Alias.Company.Tag.CompanyID] = new Guid(item[Alias.Company.Tag.CompanyID].ToString());

                if (!(item[Alias.Company.Tag.TagID] is Guid))
                    item[Alias.Company.Tag.TagID] = new Guid(item[Alias.Company.Tag.TagID].ToString());

                Dal.Add(item);

                return (Guid)item[Alias.Company.Tag.ID];
            }
            public void Modify(int id, Dictionary<string, object> changes)
            {
                throw new NotImplementedException();
            }
            public void Delete(int id)
            {
                if (CheckRight)
                {
                    Guid guid = Dal.GetGuidFromInnerID(id);
                    ValidateRight("Delete", guid);
                }

                Dal.Delete(id);
            }
            public void Delete(Guid companyID, Guid tagID)
            {
                if (CheckRight)
                {
                    ValidateRight("Delete", Guid.Empty);
                }

                Dal.Delete(companyID, tagID);
            }
            public DataTable List(int page, int pageSize, string condition, string sort)
            {
                if (CheckRight)
                {
                    ValidateRight("List", Guid.Empty);
                }

                return Dal.List(page, pageSize, condition, sort);
            }
            public int GetRecordCount(string condition)
            {
                return Dal.GetRecordCount(condition);
            }

            #region Properties
            private Dal.Company.ITag Dal
            {
                get { return (Dal.Company.ITag)DalBase; }
            }
            #endregion
        }
    }
}
